package com.zrwl.poscloud.commons.config.message;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author 刘思童
 * @create 2021-12-28 17:38
 **/
@Slf4j
@Configuration
public class WxConfig {
    @Value("${wechat.appid}")
    private String appId;
    @Value("${wechat.getTokenUrl}")
    private String getTokenUrl;

    public static String accessToken;

    public String flushToken() throws IOException {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(getTokenUrl);
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());
        postMethod.setRequestHeader("Connection", "close");
        int statusCode = httpClient.executeMethod(postMethod);
        WxConfig.accessToken = postMethod.getResponseBodyAsString();
        return WxConfig.accessToken;
    }

}
